package com.ushine.design.design.b_structural_type.g_proxy.c_example;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * java 动态代理模式
 *
 * @author yls
 */
public class Client {
    public static void main(String[] args) {
        InvocationHandler handler = null;
        AbstractuserDAO userDao = new UserDAO();
        handler = new DAOLogHandler(userDao);
        AbstractuserDAO proxy = null;
        //创建动态代理对象
        proxy = (AbstractuserDAO) Proxy.newProxyInstance(AbstractuserDAO.class.getClassLoader(),
                new Class[]{AbstractuserDAO.class}, handler);
        proxy.finUserById("张无忌");

        proxy.deleteUserById("张三");

        System.out.println("--------------------------------------");
        AbstractDocumentDAO documentDAO = new DocumentDAO();
        handler = new DAOLogHandler(documentDAO);
        AbstractDocumentDAO proxy_new = null;
        //创建动态代理对象
        proxy_new = (AbstractDocumentDAO) Proxy.newProxyInstance(AbstractDocumentDAO.class.getClassLoader(),
                new Class[]{AbstractDocumentDAO.class}, handler);
        proxy_new.deleteDocumentById("D002");
    }
}
